package com.example.xunua_zhinengjiaotong.fragment;


import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.xunua_zhinengjiaotong.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class setFragment extends Fragment implements View.OnClickListener {


    public setFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View convertView=inflater.inflate(R.layout.fragment_set, container, false);
        RelativeLayout rl_exit=convertView.findViewById(R.id.set_exit);
        TextView tv_code=convertView.findViewById(R.id.set_tv_apkcode);
        rl_exit.setOnClickListener(this);
        return convertView;
    }

    /**
     * 获取本地apk的版本号
     * @param mContext
     * @return
     */
    public static int getVersionCode(Context mContext){
        int versionCode=0;
        //获取软件版本号，对应AndroidManifest.xml中的versionCode
        try {
            versionCode = mContext.getPackageManager().getPackageInfo(mContext.getPackageName(), 0).versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionCode;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.set_exit:
                showMenu();
                break;
            case R.id.menu_btn_back:
                hiddenMenu();
                break;
            case R.id.menu_btn_ok:
                hiddenMenu();
                System.exit(0);
                break;
        }
    }

    private PopupWindow popupWindow;
    private void showMenu(){
        View contentVIew;
        contentVIew=getLayoutInflater().inflate(R.layout.item_menu,null);

        //设置activity为半透明
        WindowManager.LayoutParams lp=getActivity().getWindow().getAttributes();
        lp.alpha=0.7f;
        getActivity().getWindow().setAttributes(lp);


        popupWindow=new PopupWindow(contentVIew,ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setFocusable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setOutsideTouchable(false);//不允许点击popupWindow之外的地方
        popupWindow.setTouchable(true);
        popupWindow.showAtLocation(contentVIew, Gravity.BOTTOM,0,0);
        View view=popupWindow.getContentView();
        Button btn_back,btn_ok;
        btn_back=view.findViewById(R.id.menu_btn_back);
        btn_ok=view.findViewById(R.id.menu_btn_ok);
        btn_back.setOnClickListener(this);
        btn_ok.setOnClickListener(this);
    }
    private void hiddenMenu(){
        popupWindow.dismiss();

        //还原activity
        WindowManager.LayoutParams lp=getActivity().getWindow().getAttributes();
        lp.alpha=1.0f;
        getActivity().getWindow().setAttributes(lp);
    }
}
